<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge" ></meta>
	<title>设备租借</title>
	<link rel="stylesheet" href="${frames}/css/lib/bootstrap.min.css" type="text/css" type="text/css">
	<link rel="stylesheet" href="${frames}/css/lib/bootstrap-datetimepicker.min.css" type="text/css">
	<link rel="stylesheet" href="${newframe}/css/base/base.css" type="text/css">
	<%--<link rel="stylesheet" href="${newframe}/css/common.css" type="text/css">--%>
	<%--<link rel="stylesheet" href="${frames}/css/picker.css" type="text/css">--%>
	<link rel="stylesheet" href="${newframe}/css/select2.min.css" type="text/css">
	<%--<link rel="stylesheet" href="${newframe}/css/beautify-time.css" type="text/css">--%>
	<link rel="stylesheet" href="${assets}/css/views/rent/rentInfo.css">
	<%@ include file="/WEB-INF/jsp/component/commonBottom.jsp" %>
	<script src="${assets}/bootstrapValidator/js/bootstrapValidator.js"></script>
	<%--<script src="${frames}/laydate/laydate.dev.js"></script>--%>
	<script src="${frames}/Chronic/laydate/laydate.js"></script>
	<script src="${assets}/bootstrapValidator/js/language/zh_CN.js"></script>
	<script src="${newframe}/js/select2.full.zh.js"></script>
	<style>
		.control-label{
			text-align:left !important;
		}
		.btn{
			width:100px;
			line-height:15px;
			background:#35acfd;
			color:white;
		}
	</style>
	<script>
        $(document).ready(function() {
            // 初始化默认使劲参数
			var date = new Date();
            var year = date.getFullYear() // 年
			// debugger;
            var month = date.getMonth() + 1; // 月
			if( month < 10 ){
                month = '0' + month
 			}
            var day  = date.getDate(); // 日
			var time = year+'-'+month+'-'+day
			$('#rentTime').val(time)
			//  预计归还日期
			month = parseInt(month) + 6;
			// 判断闰年等
			if( month >12 ){
				month = '0'+ (month - 12)
			}else{
			    month = '0' + month
			}
			if(month.length > 2) {
				month = month.substring(month.length - 2, month.length);
			}
			console.log(month);
			if( year%4 != 0 ){
                // 平年
				if( month == 2 ){
                    if( day > 28 ){
                        day = 28
					}
				}else if( month == 4 || month == 6 || month == 9 || month == 11 ){
                    if( day > 30 ){
                        day = 30
					}
				}else{
				    day = 31
				}
			}else{
                if( month == 2 ){
                    if( day > 28 ){
                        day = 29
                    }
                }else if( month == 4 || month == 6 || month == 9 || month == 11 ){
                    if( day > 30 ){
                        day = 30
                    }
                }else{
                    day = 31
                }
			}
			var rentime = year+'-'+month+'-'+day
			$('#expectBackTime').val(rentime)
			$('#rentMoney').val('0')
            $('#myForm').bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    instrumentId: {
                        validators: {
                            notEmpty: true
                        }
                    },
                    rentMan: {
                        validators: {
                            notEmpty: true
                        }
                    },
                    rentTel: {
                        validators: {
                            notEmpty: true
                        }
                    },
                    rentTime: {
                        validators: {
                            notEmpty: true
                        }
                    },
                    rentMoney: {
                        validators: {
                            notEmpty: true,
                            numeric: true
                        }
                    },
                    rentInterval: {
                        validators: {
                            stringLength:{
                                min: 0,
                                max: 10
                            }
                        }
                    },
                    expectBackTime: {
                        validators: {
                            notEmpty: true
                        }
                    },
                    idCard: {
                        validators: {
                            notEmpty: true
                        }
                    }
                    <shiro:hasPermission name="super:role">
                    ,
                    hospId: {
                        validators: {
                            notEmpty: true
                        }
                    }
                    </shiro:hasPermission>

                }
            }).on('success.form.bv', function (e) {
                e.preventDefault();
                $.ajax({
                    url:'${URL_RENT_ADDRENT}',
                    type:'post',
                    data:$('#myForm').serialize(),
                    async : false, //默认为true 异步
                    error:function(){
                        alert('error');
                    },
                    success:function(data){
                        var d = eval( '(' + data + ')');
                        if(!d.success){
                            alert(d.msg);
                        }else{
                            console.log(JSON.parse(data).data)
                            var rentId = JSON.parse(data).data.rentId
                            console.log(rentId)
                            $.ajax({
                                url:"${URL_SCREENING_GETBREADLINE}",
                                dataType:'json',
                                type:'post',
                                success:function(res){
//                                    var url = JSON.parse(res.data)[1].href+'?id='+ rentId
//                                    var r = confirm("是否打印？")
                                    window.parent.showNewModelIframe('${URL_RENT_TOPRINT}?id='+rentId ,300,150)
                                    closeFrame()
//                                    if(r){
//                                        closeFrame()
//                                        parent.showSrc(url)
//                                    }else{
//                                        closeFrame()
//                                        parent.showSrc(JSON.parse(res.data)[1].href)
//                                    }
                                }
                            })
                        }
                    }
                });
            });
            initLayDate("#rentTime");
            initLayDate("#expectBackTime");
            initInstr("#instrumentId");
            <shiro:hasPermission name="super:role">
            initHosptipal("#hospId");
            </shiro:hasPermission>
            initDoctor("#doctorId");
        });

        function initLayDate(id) {
            laydate({
                elem: id,
                istime: true,
                format: 'YYYY-MM-DD',
                choose: function (dates) { //选择好日期的回调
                    $('#myForm').bootstrapValidator('revalidateField', $(id));
                    // initTablePatient();
                }
            })
        }




        function closeFrame(){
            window.parent.closeModal();
        }
        function initDateTime(id){
            var t = $(id).datetimepicker({
                language:  'zh-CN',
                weekStart:  0,
                todayBtn: true,
                autoclose:  1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0
            }).on('hide',function(e) {
                $('#myForm').bootstrapValidator('revalidateField', id);
            });
        }
        function initInstr(id){
            $(id).select2({
                placeholder: '请选择设备',
                ajax: {
                    url: '${URL_BASE_SELECTINSTRUMENT}',
                    dataType: 'json',
                    type: "post",
                    delay: 250,
                    data: function (params) {
                        var data=new Object();
                        data["limit"]=10;
                        if(params.page==undefined){
                            data["offset"]=0;
                        }else {
                            data["offset"]= (params.page-1)*10;
                        }
                        if(params.term!=undefined){
                            data["name"]= params.term;
                        }
                        return data;
                    },
                    processResults: function (data, params) {
                        params.page = params.page || 1;
                        return {
                            results: data.data,
                            pagination: {
                                more: (params.page * 10) < data.msg
                            }
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                minimumInputLength: 0,
                templateResult: formatRepo, // omitted for brevity, see the source of this page
                templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
            });
        }
        function formatRepo (repo) {
            if (repo.loading) return repo.text;
            var markup ="<div class='select2-result-repository clearfix'>" +
                "<div class='select2-result-repository__title'>" + repo.text+'|'+repo.instrumentSN + "</div></div>";
            return markup;
        }

        function formatRepoSelection (repo) {
            $("#instrumentId").val(repo.id);
            return repo.text;
        }



        //医院下拉
        function formatRepoHosp(repo) {
            if (repo.loading) {
                return repo.text;
            }
            var markup = "<div class='select2-result-repository clearfix'>" +
                "<div class='select2-result-repository__title'>" + repo.text +"</div></div>";
            return markup;
        }
        function formatRepoSelectionHosp(repo) {
            $("#hospId").val(repo.id);
            return repo.text;
        }
        function initHosptipal(id){
            $(id).select2({
                placeholder: '请选择医院',
                ajax: {
                    url: '${URL_BASE_SELECTHOSPITAL}',
                    dataType: 'json',
                    type: "post",
                    delay: 250,
                    data: function (params) {
                        var data=new Object();
                        data["limit"]=10;
                        if(params.page==undefined){
                            data["offset"]=0;
                        }else {
                            data["offset"]= (params.page-1)*10;
                        }
                        if(params.term!=undefined){
                            data["name"]= params.term;
                        }
                        return data;
                    },
                    processResults: function (data, params) {
                        params.page = params.page || 1;
                        return {
                            results: data.data,
                            pagination: {
                                more: (params.page * 10) < data.msg
                            }
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                minimumInputLength: 0,
                templateResult: formatRepoHosp, // omitted for brevity, see the source of this page
                templateSelection: formatRepoSelectionHosp // omitted for brevity, see the source of this page
            });
        }
        //医生下拉
        function formatRepoDoctor(repo) {
            if (repo.loading) {
                return repo.text;
            }
            var markup = "<div class='select2-result-repository clearfix'>" +
                "<div class='select2-result-repository__title'>" + repo.text +"</div></div>";
            return markup;
        }
        function formatRepoSelectionDoctor(repo) {
            $("#doctorId").val(repo.id);
            return repo.text;
        }
        function initDoctor(id){
            $(id).select2({
                placeholder: '请选择医生',
                ajax: {
                    url: '${URL_BASE_SELECTDOCTOR}',
                    dataType: 'json',
                    type: "post",
                    delay: 250,
                    data: function (params) {
                        var data=new Object();
                        data["iDisplayLength"]=10;
                        if(params.page==undefined){
                            data["iDisplayStart"]=0;
                        }else {
                            data["iDisplayStart"]= (params.page-1)*10;
                        }
                        if(params.term!=undefined){
                            data["drName"]= params.term;
                        }

                        return data;
                    },
                    processResults: function (data, params) {
                        params.page = params.page || 1;
                        return {
                            results: data.data,
                            pagination: {
                                more: (params.page * 10) < data.msg
                            }
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                minimumInputLength: 0,
                templateResult: formatRepoDoctor, // omitted for brevity, see the source of this page
                templateSelection: formatRepoSelectionDoctor // omitted for brevity, see the source of this page
            });
        }

	</script>
</head>
<body style="width: 98%">
<div class="header">
	<h1>设备租借</h1>
</div>
<form id="myForm" class="form-horizontal" method="post" >
	<div class="clearfix" style="text-align:left;">
		<div class="form-group mg">
			<label class="control-label">设备</label>
			<select name="instrumentId" id="instrumentId"  class="form-control">

			</select>
		</div>
		<div class="form-group">
		<label class="control-label">租借人</label>
		<input type="text" id="rentMan" name="rentMan" class="form-control" >
		</div>
		<div class="form-group mg">
			<label class="control-label">租借人身份证号</label>
			<input type="text" id="idCard" name="idCard" class="form-control" style="padding-right: 30px;">
		</div>
		<div class="form-group">
		<label class="control-label">租借人电话</label>
		<input type="text" id="rentTel" name="rentTel" class="form-control" >
		</div>
		<%--<div class="form-group">--%>
		<%--<label class="control-label">签约团队</label>--%>
		<%--<input type="text" id="signTeam" name="signTeam" class="form-control" >--%>
		<%--</div>--%>
		<div class="form-group mg">
		<label class="control-label">租借日期</label>
		<div id="TimeArea" class="input-group date form_date">
		<input type="text" id="rentTime" name="rentTime" class="form-control" placeholder="选择日期" readonly>
		</div>
		</div>
		<div class="form-group">
		<label class="control-label">已付押金</label>
		<input type="text" id="rentMoney" name="rentMoney" class="form-control" >
		</div>
		<div class="form-group mg">
		<label class="control-label">预归还日期</label>
		<div class="input-group date form_date">
		<input type="text" id="expectBackTime" name="expectBackTime" class="form-control" placeholder="选择日期" readonly>
		</div>
		</div>
		<div class="form-group">
		<label class="control-label">租借备注</label>
		<input type="text" id="rentComment" name="rentComment" class="form-control" >
		</div>
		<%--所属医院--%>
		<shiro:hasPermission name="super:role">
			<div class="form-group">
				<label class="control-label">所属医院</label>
				<select name="hospId" id="hospId"  class="form-control"></select>
			</div>
		</shiro:hasPermission>
		<shiro:lacksPermission name="super:role">
		    <input type="hidden" id="hospId" name="hospId" class="form-control" value="${hospital.id}">
		</shiro:lacksPermission>
	</div>
	<div class="form-group submitBtn">
		<button type="button" class="btn" data-dismiss="modal" id="closeModalButt" onclick="closeFrame()">关闭</button>
		<button type="submit" name="submit" class="btn">保存</button>
	</div>
</form>
</body>
</html>